<?php

namespace App\Http\Controllers\Email;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Mail;
use Validator;
use Illuminate\Support\Facades\Redis;
class EmailController extends Controller
{
    //
    public function EmailSend(Request $request)
    {
        if (Redis::exists('code')) {
            $code = Redis::get('code');
            if($code!=$request->VarCode)
            {
                session()->flash('false', '验证码错误!');
                return redirect()->back();
            }
        }
        else{
            session()->flash('false', '验证码为空!');
                return redirect()->back();
        }
        $validator = Validator::make($request->all(), [
            'title' => 'required|20',
            'name' => 'required|max:10',
            'email' => 'required|email',
            'content' => 'required|max:2000',
            'tel' => 'required|min:11|max:13',
            'VarCode'=>'required',
        ]);
        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 401);
        }
       
        $view = '';
        $data['tel'] = $request->tel;
        $data['name'] = $request->name;
        $data['email'] = $request->email;
        $data['content'] = $request->content;
        $data['title'] = $request->title;
        $to = '2827204774@qq.com';
        $subject = "请查收邮件！";
        $rs=Mail::send($view, $data, function ($message) use ($to, $subject) {
            $message->to($to)->subject($subject);
        });
        if($rs){
            session()->flash('true', '发送成功!'); 
            $data = ['success' => ture, 'message' => '发送成功!'];
            return json_encode($data);
        }
        else{
            session()->flash('false', '格式错误!');
            $data = ['success' => false, 'message' => '格式错误'];
            return  json_encode($data);
        }
    }
    public function VarCodeSend(Request $request)
    {
       
        $validator = Validator::make($request->all(), [
         'email'=>'required|email',
            ]);
            if ($validator->fails()) {
                return response()->json(['error' => $validator->errors()], 401);
            }
        $strs = "QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm";
        $code = substr(str_shuffle($strs), mt_rand(0, strlen($strs) - 11), 6);
        Redis::set('code', $code);
        $to = $request->email;
        $view='re';
        $data['code']=$code;
        $subject="请查收验证码，如果不是本人的操作，请忽略此邮件！";
        $rs=Mail::send($view, $data, function ($message) use ($to, $subject) {
            $message->to($to)->subject($subject);

        });
        if($rs){
            session()->flash('true', '发送成功!'); 
            $data = ['success' => ture, 'message' => '发送成功!'];
            return json_encode($data);
        }
        else{
            session()->flash('false', '格式错误!');
            $data = ['success' => false, 'message' => '格式错误'];
            return  json_encode($data);
        }
    }
}
